package com.iterator.controller.wechat.lyh;

import com.iterator.bean.web.lyh.utils.BaseRespVO;
import com.iterator.bean.wechat.lyh.WxDataFootprintVO;
import com.iterator.service.wechat.lyh.WxFootprintService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wx/footprint")
public class WxFootprintController {

    @Autowired
    WxFootprintService wxFootprintService;

    @RequestMapping("list")
    //用户足迹列表
    public Object getFootprintList(@RequestParam("page") Integer page,
                                       @RequestParam("size") Integer size) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        WxDataFootprintVO dataFootprintVO =
                wxFootprintService.getFootprintList(username, page, size);
        return BaseRespVO.ok(dataFootprintVO);
    }

    @RequestMapping("delete")
    //删除特定足迹表id的用户足迹
    public BaseRespVO deleteFootprint(@RequestBody Map delete) {
        //这里即使不使用username也可以将指定足迹表id的deleted更改为1
        //但是这样可能会导致别人可以使用postman这种工具删除不是自己的用户足迹
        //另外,即使用postman传入错误id也没关系,反正删除个寂寞
        Integer id = (Integer) delete.get("id");
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        Integer code = wxFootprintService.deleteFootprint(username, id);
        if (code == null || code == 0) {
            return BaseRespVO.fail(777, "无记录");
        }
        return BaseRespVO.ok();
    }

}
